<?php


namespace app\admin\controller\tickets;
use app\common\controller\Adminbase;
use think\Db;

class Tickets extends Adminbase
{
    protected $searchFields = 'id,username,nickname';

    protected function initialize()
    {
        parent::initialize();
        $goods_categoty = Db::name('goods_categoty')
            ->where('is_del', '<>', 1)
            ->order('sort desc')
            ->select();
        foreach ($goods_categoty as $k => $v) {
            $goods_categoty[$k]['goods'] = Db::name('goods')
                ->where('is_del','<>',1)
                ->where('cate_id','=',$v['id'])
                ->where('is_ticket','=',1)
                ->select();
        }
        $this->assign('goodsCategoty', $goods_categoty);
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $_list = Db::name('ticket_category')
                ->alias('tc')
                ->where('tc.is_del', '<>', 1)
                ->field('tc.*,goods.name as goods_name,goods.specs,gc.name as gc_name')
                ->join('goods','goods.id=tc.goods_id')
                ->join('goods_categoty gc','gc.id=tc.goods_cate')
                ->order('sort')
                ->page($page, $limit)
                ->select();
            $total = Db::name('ticket_category')
                ->where('is_del', '<>', 1)
                ->count();
            //销售数量 核销数量 待使用数量
            foreach ($_list as $k=>$v){
                $sale_num=Db::name('member_ticket_history')
                    ->where('ticket_cate','=',$v['id'])
                    ->where('type','=',1)
                    ->sum('num');
                $write_num=Db::name('member_ticket_history')
                    ->where('ticket_cate','=',$v['id'])
                    ->where('type','=',2)
                    ->sum('num');
                $_list[$k]['sale_num']=$sale_num;
                $_list[$k]['write_num']=$write_num;
                $_list[$k]['detail_write_num']=$sale_num-$write_num;
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('ticket_category')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('ticket_category')->where('id', $id)->update([
                    'is_del' => 1
                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['goods_id']=explode(',', $data['goods_id']);
            $where[] = ['goods_id', '=', $data['goods_id'][1]];
            $where[] = ['is_del', '<>', 1];
            $ticket_category = Db::name('ticket_category')
                ->where($where)
                ->find();

            if ($ticket_category) {
                $this->error('关联商品已存在');
            }
            $insert_data = array(
                'name' => $data['name'],
                'img' => $data['img'],
                'sort'=>$data['sort'],
                'is_del' => 0,    //'是否删除 1 是 0 不是',
                'status'=>1,//上架
                'goods_cate'=>$data['goods_id'][0],
                'goods_id'=>$data['goods_id'][1],
            );
            $stmt = Db::name('ticket_category')->insertGetId($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        } else {
            return $this->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data=$this->request->Post();
            $data['goods_id']=explode(',', $data['goods_id']);
            $insert_data = array(
                'id'=>$data['id'],
                'name' => $data['name'],
                'img' => $data['img'],
                'sort'=>$data['sort'],
                'goods_cate'=>$data['goods_id'][0],
                'goods_id'=>$data['goods_id'][1],
            );
            $stmt = Db::name('ticket_category')->update($insert_data);
            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }else{
            $id = $this->request->param('id/d', 0);
            $data = Db::name('ticket_category')->find($id);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

}